package ru.evga.billboard.document;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


public abstract class DocPart
    implements IDocPart
{
    private IDocPart parent;
    private List<IDocPart> children = new ArrayList<IDocPart>();

    
    public DocPart(IDocPart parentDocPart)
    {
        parent = parentDocPart;
        if (parent != null)
        {
            parent.addChild(this);
        }
    }

    public IDocPart getParent()
    {
        return parent;
    }

    public void setParent(IDocPart parent)
    {
        this.parent = parent;
    }

    public void handleEvent(DocPartEvent event)
    {
        Collection<IDocPart> children = getChildren();
        for (IDocPart child : children)
        {
            child.handleEvent(event);
        }
    }

    public boolean addChild(IDocPart model)
    {
        model.setParent(this);
        return children.add(model);
    }

    public Collection<IDocPart> getChildren()
    {
        return Collections.unmodifiableList(children);
    }

    public boolean removeChild(IDocPart model)
    {
        return children.remove(model);
    }

    @Override
    public void read(InputStream inputStream) throws IOException
    {
    }

    @Override
    public void write(OutputStream outputStream) throws IOException
    {
    }
}
